<?php

/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Mail.php 5337 2010-09-07 08:18:38Z huuphuoc $
 * @since		2.0.6
 */
class Mail_Models_Mail extends Rabbit_Db_Table_Abstract implements Mail_Models_Interfaces_Mail {

    protected $_name = 'mail';

    public function convert($entity) {
        return new Mail_Models_Entities_Mail($entity);
    }

    public function  getById($id) {
        $row = $this->getAdapter()
                ->select()
                ->from(array('m' => $this->_name))
                ->where('m.id = ?', $id)
                ->limit(1)
                ->query()
                ->fetch();
        return ($row == null) ? null : new Mail_Models_Entities_Mail($row);
    }
    public function getMails($offset = null, $count = null, $exp = null) {
        $select = $this->getAdapter()
                ->select()
                ->from(array('m' => $this->_prefix . 'mail'));

        if (is_int($offset) && is_int($count)) {
            $select->order('m.created_date DESC')
                    ->limit($count, $offset);
        }
        if ($exp['type'] && isset($exp['type'])) {
            $select->where('type = ?', $exp['type']);
        }
        if ($exp['readed']) {
            $select->where('readed = ?', $exp['readed']);
        }
        if ($exp['created_user_id']) {
            $select->where('created_user_id = ?', $exp['created_user_id']);
        }
        $rs = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function count($exp = null) {
        $select = $this->getAdapter()
                ->select()
                ->from(array('m' => $this->_prefix . 'mail'), array('num_mails' => 'COUNT(*)'))
                ->limit(1);
        if ($exp['type'] && isset($exp['type'])) {
            $select->where('type = ?', $exp['type']);
        }
        if ($exp['readed']) {
            $select->where('readed = ?', $exp['readed']);
        }
        if ($exp['created_user_id']) {
            $select->where('created_user_id = ?', $exp['created_user_id']);
        }
        return $select->query()->fetch()->num_mails;
    }

    public function add($mail) {
        $this->getAdapter()->insert($this->_prefix . 'mail', array(
            'template_id' => $mail->template_id,
            'subject' => $mail->subject,
            'content' => $mail->content,
            'created_user_id' => $mail->created_user_id,
            'from_mail' => $mail->from_mail,
            'from_name' => $mail->from_name,
            'reply_to_mail' => $mail->reply_to_mail,
            'reply_to_name' => $mail->reply_to_name,
            'to_mail' => $mail->to_mail,
            'to_name' => $mail->to_name,
            'status' => $mail->status,
            'created_date' => $mail->created_date,
            'sent_date' => $mail->sent_date,
        ));
        return $this->getAdapter()->lastInsertId($this->_prefix . 'mail');
    }
    public function updateRead($id){
        return $this->update(array('is_read' => 1), array('id = ?' => $id));
        
    }

    public function delete($id){
        return parent::delete('id = ' . $id);
    }

}
